package olSystem;

import com.spm.utils.StudentMapperUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;


@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public  RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request,response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        //StudentService st = new StudentService();
        String username = request.getParameter("username");
        String id = request.getParameter("id");
        int age = Integer.parseInt(String.valueOf(request.getParameter("age")));
        String sclass = request.getParameter("sclass");
        String password = request.getParameter("password");
        String password1 = request.getParameter("password1");
        String sex = request.getParameter("sex");
        String message = "";
        //得到HttpSession类型的对象
        HttpSession session = request.getSession();
        Student user=new Student();
        if (password.equals(password1)) {

//            st.setUsername(username);
//            st.setId(id);
//            st.setSclass(sclass);
//            st.setPassword(password);
//            st.setSex(sex);
//            user.setName(username);
//            user.setNo(id);
//            user.setSclass(sclass);
//            user.setPasswd(password);
//            user.setSex(sex);
//            user.setAge(age);
            StudentMapperUtil studentMapperUtil = new StudentMapperUtil();
            int i=studentMapperUtil.insertStudent(new com.spm.pojo.Student(id,username,password,age,sex,sclass));
            if(i!=0){
                out.print("注册成功！");
                response.sendRedirect("./login.jsp");
            }
            else{
                out.print("注册失败！");
                response.sendRedirect("./register.jsp");
            }
//            session.setAttribute("id", id);
//            session.setAttribute("username", username);
//            session.setAttribute("password", password);
//            session.setAttribute("sex", sex);
//            if( !UserDB.UserExists(username) ) {
//            message = "注册成功";
//            session.setAttribute("message", message);
//                //将注册用户信息写入数据库
//                UserDB.insert(user);
//            response.sendRedirect("http://localhost:8080/cjw_war_exploded/Olsystem/regok.jsp");
//        } else {
//            message = "密码输入错误";
//            session.setAttribute("message", message);

        }
//        olSystem.StudentDao dao = new olSystem.StudentDao();
//        try {
//            Connection conn = dao.getConnection();
//            if(conn!=null) {
//                out.print("conn is not null");
//            }
//            if(dao.addUser(user)){
//                out.print("注册成功！");
//                response.sendRedirect("./login.jsp");
//            }
//            else{
//                out.print("注册失败！");
//                response.sendRedirect("./register.jsp");
//            }
//        } catch (SQLException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
    }

}
